*
clear
cap clear matrix
set mem 4000m
set more off
*
**************
*productivity*
**************
use brn_eae, clear
gen n16 = substr(naf,1,2)
bysort n16 year: egen p1_pty=pctile(laborpty),p(1)
bysort n16 year: egen p99_pty=pctile(laborpty),p(99)
replace llaborpty =. if (laborpty>p99_pty | laborpty<p1_pty) & llaborpty!=.
replace llaborpty_def =. if (laborpty>p99_pty | laborpty<p1_pty) & llaborpty!=.
sort firmid year
save brntemp,replace
*
**************************************************
*merge trade flows with industry classification  *
*and keep only manufacturing + balance sheet data*
**************************************************
use data/MMO_tradedata,clear
drop if export == 0
rename iso2 iso2_d
sort iso2_d year
merge iso2_d year using fragrav
tab _merge
tab iso2_d if _merge == 1
drop if _merge !=3
keep firmid year nc8 export qty iso2_d
gen hs6 = int(nc8/100)
compress
sort firmid year
merge firmid year using brntemp
tab _merge
keep if  _merge == 3
drop _merge
*
keep if manuf==1
*
compress
sort year firmid iso2_d
save MMO_C_regfile, replace
erase brntemp.dta
*
*****************************************************
*Collect info on ISIC of export by firmid year,  iso*
*****************************************************
use data/hs6-isic, clear
duplicates report hs6 isic
duplicates drop hs6 isic, force
duplicates tag hs6, gen (test)
list if test == 1
bysort hs6: keep if _n == 1
drop test
sort hs6
save data/hs6-isic_clean,replace
*
use MMO_C_regfile, clear
sort hs6
merge hs6 using data/hs6-isic_clean
tab _merge
keep if _merge == 3
drop _merge w
label var isic "ISIC of export"
save MMO_C_regfile, replace
compress
save MMO_tradebrn, replace
***********This file is the one that has all dimensions: firm-HS-country-year
***********with export values and firm-level balance - sheet data, to be used
***********when describing entry patterns for instance.
* This is computing the ISIC level share/rank of exports
collapse (sum) export, by(firmid year isic iso2_d)
egen nbisic = count(isic), by(firmid year iso2_d)
egen export_iso = sum(export), by(firmid year iso2_d)
gen export_isicshare = export/export_iso
drop export_iso
gsort firmid year iso2_d -export
by firmid year iso2_d : gen export_isicrank = _n
label var nbisic "count of isic exported by firm-year-iso"
label var export_isicshare "share of isic exported by firm-year-iso"
label var export_isicrank "rank of isic exported by firm-year-iso"
sort firmid year iso2_d isic
save tempmainisic,replace
*
use MMO_C_regfile, clear
sort firmid year iso2_d isic
merge firmid year iso2_d isic using tempmainisic
tab _merge
drop _merge
*
sort year firmid iso2_d
save MMO_C_regfile, replace
*
erase tempmainisic.dta
*
*****************************************************************
*getting information about the rank and number of product exports
use MMO_C_regfile, clear
collapse (sum) export , by(firmid year nc8 isic) fast
collapse (count) nbnc8isic = nc8, by(firmid year isic)
label var nbnc8isic " nb distinct nc8 within ISIC by firm/year"
keep firmid year isic nbnc8isic
save tempnc8isic,replace
*
use MMO_C_regfile, clear
collapse (sum) export , by(firmid nc8 isic) fast
collapse (count) nbnc8_max_isic = nc8, by(firmid isic)
label var nbnc8_max_isic " nb distinct nc8 within ISIC exported by firm over the whole period "
keep firmid isic nbnc8_max_isic
save tempnc8maxisic,replace
*
use MMO_C_regfile, clear
collapse (sum) export , by(firmid nc8) fast
collapse (count) nbnc8_max = nc8, by(firmid)
label var nbnc8_max " nb distinct nc8 exported by firm over the whole period "
keep firmid nbnc8_max
save tempnc8max,replace
*
use MMO_C_regfile, clear
collapse (sum) export , by(firmid year nc8)
egen nbnc8 = count(nc8), by(firmid year)
egen exportranknc8 = rank(export), by(firmid year) field
label var exportranknc8 "rank of nc8 export value by firm/year"
rename  export exportnc8
label var exportnc8 "export value by firm/year/nc8 (summed over all countries)"
label var nbnc8 " nb nc8 exported by firm/year"
gen nc2 = int(nc8/1000000)
egen exportranknc8i = rank(exportnc8), by(firmid year nc2) field
label var exportranknc8i "rank of nc8 export value by firm/year/nc2"
drop nc2
merge m:1 firmid using tempnc8max
drop _merge
save MMO_C_regfile_nc8, replace
*
*merging back
use MMO_C_regfile,clear
merge m:1 firmid year nc8 using MMO_C_regfile_nc8
drop _merge
*
merge m:1 firmid year isic using tempnc8isic
drop _merge
*
merge m:1 firmid isic using tempnc8maxisic
drop _merge
save MMO_C_regfile, replace
erase tempnc8maxisic.dta
erase tempnc8max.dta
erase tempnc8isic.dta
*
*******************************************************
*getting information about the rank of product in terms of # countries exported to
qui bysort  firmid year nc8: gen iso2num = _n
collapse (count) isonumb=iso2num , by(firmid year nc8)
egen ctryranknc8 = rank(isonumb), by(firmid year) field
label var isonumb "nc8 # of iso2 by firm/year"
label var ctryranknc8 "rank of nc8 # of iso2 by firm/year"
sort firmid year nc8
save MMO_C_regfile_isonumb, replace
*merging back
use MMO_C_regfile,clear
sort firmid year nc8
merge firmid year nc8 using MMO_C_regfile_isonumb
tab _merge
drop _merge
*sort year firmid exportrank iso2_d
save MMO_C_regfile, replace
erase MMO_C_regfile_isonumb.dta
*
********************************
*generate nb exporters variable*
********************************
qui bysort firmid iso2_d year: keep if _n == 1
collapse (count) nbexporters = firmid, by(iso2_d year)
label var nbexporters "nb exporters by country/year"
sort iso2_d year
save temp,replace
clear
use MMO_C_regfile
sort iso2_d year
merge iso2_d year using  temp
tab _merge
drop _merge
erase temp.dta
*
egen nbnc8iso2 = count(nc8), by(firmid iso2_d year)
label var nbnc8iso2 "nb nc8 exported by firm/country/year"
egen nbnc8iso2isic = count(nc8), by(firmid iso2_d year isic)
label var nbnc8iso2isic "nb nc8 exported by firm/country/year/isic"
*
gen nc2 = int(nc8/10000)
label var nc2 "Product - nc2 classification"
*
save MMO_C_regfile, replace
*
use MMO_C_regfile,clear
save MMO_C_regfilefull,replace
*
*********************************************************
*Assembly of "core" data is finished, now start skewness*
*Trade shock construction also uses this MMO_C_regfilefull
*********************************************************
*
*********************************************
*1)ratio of first to second product exported
*defined "locally" and "globally"
*
egen exportranki = rank(export), by(firmid year iso2_d isic nc2) field
egen exportrank = rank(export), by(firmid year iso2_d isic) field
egen exportiso2 = sum(export), by(firmid year iso2_d isic)
label var exportiso2 "export value by firm/year/iso2"
*
drop if exportrank>3 & exportranknc8>3 & exportranki>3 & exportranknc8i>3
egen exportiso2_3prod = sum(export), by(firmid iso2_d)
count
*Local
sort firmid year iso2_d isic exportrank
qui by firmid year iso2_d isic : gen lratio = ln(export[1]/export[2])
qui by firmid year iso2_d isic : gen mshare = export[1]/exportiso2 if nbnc8iso2isic>2
qui by firmid year iso2_d isic : gen lratio13 = ln(export[1]/export[3])
qui by firmid year iso2_d isic : gen lratio23 = ln(export[2]/export[3])
*
*Global
sort firmid year iso2_d isic exportranknc8
global cond "(exportranknc8[1]==1 & exportranknc8[2]==2)"
qui gen uv = export/qty
qui by firmid year iso2_d isic : gen lratiow = ln(export[1]/export[2]) if $cond
qui by firmid year iso2_d isic : gen lratiopw = ln(uv[1]/uv[2]) if $cond
global cond "(exportranknc8[1]==1 & exportranknc8[3]==3)"
qui by firmid year iso2_d isic : gen lratiow13 = ln(export[1]/export[3]) if $cond
*
*Global with # countries (rank of export value is the tie breaker)
sort firmid year iso2_d isic ctryranknc8 exportranknc8
global cond "(ctryranknc8[1]==1 & (ctryranknc8[2]==1|ctryranknc8[2]==2))"
qui by firmid year iso2_d isic : gen lratiowc = ln(export[1]/export[2]) if $cond
qui by firmid year iso2_d isic : gen lratiopwc = ln(uv[1]/uv[2]) if $cond
global cond "(ctryranknc8[1]==1 & ctryranknc8[3]==3)"
qui by firmid year iso2_d isic : gen lratiow13c = ln(export[1]/export[3]) if $cond
*
bysort firmid year iso2_d isic : keep if _n == 1
drop nc8 hs6 export qty
save temp,replace
*
********************************************************************
*2)using entropy (theil in fact) as a measure of skewness of sales
use MMO_C_regfile,clear
keep if nbnc8iso2isic>2
*keeping only firms that export at least 2 products per country
*Theil index by iso2
egen exportmiso2 = mean(export), by(firmid year iso2_d isic)
egen exportgiso2 = gmean(export), by(firmid year iso2_d isic)
egen exportsiso2 = sum(export), by(firmid year iso2_d isic)
*Atkinson index for eps=eps10/10
foreach eps10 in 5 20 {
gen export`eps10' = export^(1-`eps10'/10)
egen exportm`eps10'iso2 = mean(export`eps10'), by(firmid year iso2_d isic)
gen atk`eps10' = 1 - (exportm`eps10'iso2^(1/(1-`eps10'/10)))/exportmiso2
}
gen atk10 = 1 - (exportgiso2/exportmiso2)
*Theil
gen sharenc8 = export / exportsiso2
egen theil = sum(sharenc8*ln(export/exportmiso2)), by(firmid year iso2_d isic)
*
*Entropy
egen entrop = sum(-sharenc8*ln(sharenc8)), by(firmid year iso2_d isic)
*Theil defined over all exported products
gen theil0t = ln(nbnc8_max_isic)-entrop
gen theil0 = ln(nbnc8isic)-entrop
*Herfindhal and s.d by iso2
egen herf = sum(sharenc8^2), by(firmid year iso2_d isic)
egen sdlx = sd(ln(export)), by(firmid year iso2_d isic)
drop sharenc8
*
*Theil change holding constant number of products
egen firmisoisic = group(firmid iso2_d isic)
egen firmisonc8 = group(firmid iso2_d isic nc8)
tsset firmisonc8 year
gen Zint = export*ln(export)
gen Zintlag = L.export*ln(L.export)
gen exportlag = L.export
foreach eps10 in 5 20 {
gen export`eps10'lag = L.export`eps10'
}
*
drop if Zint==. | Zintlag ==.
*
egen Nprodconst= count(Zint), by(firmisoisic year)
egen Nprodconstlag= count(Zintlag), by(firmisoisic year)
*
egen Z= sum(Zint), by(firmisoisic year)
egen Zlag= sum(Zintlag), by(firmisoisic year)
egen X= sum(export), by(firmisoisic year)
egen Xlag= sum(exportlag), by(firmisoisic year)
*
gen theil_dconst =  Z/X - Zlag/Xlag - ln(X) + ln(Xlag)
replace theil_dconst = . if (Nprodconst!=Nprodconstlag) |  Nprodconst<3
label var theil_dconst "local Theil change (holding number of products constant)"
*
*Atkinson index for eps=eps10/10
foreach eps10 in 5 20 {
egen exportm`eps10'iso2_dconst = mean(export`eps10'), by(firmisoisic year)
egen exportm`eps10'iso2_dconstlag = mean(export`eps10'lag), by(firmisoisic year)
gen atk`eps10'_dconst = (exportm`eps10'iso2_dconstlag^(1/(1-`eps10'/10)))/(Xlag/Nprodconstlag) ///
                      -  (exportm`eps10'iso2_dconst^(1/(1-`eps10'/10)))/(X/Nprodconst)
replace atk`eps10'_dconst = . if (Nprodconst!=Nprodconstlag) |  Nprodconst<3
}
*
egen exportgiso2_dconst = gmean(export) , by(firmisoisic year)
egen exportgiso2_dconstlag = gmean(exportlag) , by(firmisoisic year)
gen atk10_dconst = (exportgiso2_dconstlag)/(Xlag/Nprodconstlag) ///
                      -  (exportgiso2_dconst)/(X/Nprodconst)
replace atk10_dconst = . if (Nprodconst!=Nprodconstlag) |  Nprodconst<3
*
bysort firmisoisic year : keep if _n == 1
*
tsset firmisoisic year
gen theil_d1 = D.theil
reg theil_dconst theil_d1
*
keep firmid year iso2_d isic theil theil0 theil0t theil_dconst entrop herf sdlx nbnc8iso2 nbnc8iso2isic  Nprodconst atk*
sort firmid year iso2_d isic
save temp2,replace
*
*
*********************************************************
*3)ratio of first to second product exported , WORLDWIDE*
use MMO_C_regfile_nc8,clear
*
drop if exportranknc8>3 & exportranknc8i>3
gen nc2 = int(nc8/10000)
*
*Global
sort firmid year exportranknc8
global cond "(exportranknc8[1]==1 & exportranknc8[2]==2)"
qui by firmid year : gen lratioww = ln(exportnc8[1]/exportnc8[2]) if $cond
global cond "(exportranknc8[1]==1 & exportranknc8[3]==3)"
qui by firmid year : gen lratioww13 = ln(exportnc8[1]/exportnc8[3]) if $cond
*
label var lratioww "ln ratio 1st to 2nd sales worldwide"
label var lratioww13 "ln ratio 1st to 3rd sales worldwide"
bysort firmid year : keep if _n == 1
keep firmid year lratio*
sort firmid year
save temp3,replace
********************************************************************
*4)using entropy (theil in fact) as a measure of skewness of sales WOrLDWIDE
use MMO_C_regfile_nc8,clear
keep if nbnc8>2
*keeping only firms that export at least 2 products
*
gen nc2 = int(nc8/1000000)
*
egen exportmw = mean(exportnc8), by(firmid year)
egen exportsw = sum(exportnc8), by(firmid year)
*
*Atkinson index for eps=eps10/10
foreach eps10 in 2 5 15 {
gen export`eps10'w = exportnc8^(1-`eps10'/10)
egen exportm`eps10'w = mean(export`eps10'w), by(firmid year)
gen atk`eps10'w = 1 - (exportm`eps10'w^(1/(1-`eps10'/10)))/exportmw
}
*
*Theil index worldwide
gen sharenc8 = exportnc8 / exportmw
egen theil1 = sum(sharenc8*ln(sharenc8)), by(firmid year)
gen theilw = 1/nbnc8 * theil1
drop theil1 sharenc8
*
*Entropy worldwide
gen sharenc8 = exportnc8 / exportsw
egen entrop1 = sum(sharenc8*ln(sharenc8)), by(firmid year)
gen entropw = - entrop1
drop entrop1 sharenc8
*Theil defined over all exported products
gen theilw0 = ln(nbnc8_max)-entropw
*
*Herfindhal and s.d by iso2
gen sharenc8 = exportnc8 / exportsw
egen herfw = sum(sharenc8^2), by(firmid year)
*egen sdlxw = sd(ln(export)), by(firmid year)
drop sharenc8 exportmw exportsw
*
*Theil change holding constant number of products
egen firmnc8 = group(firmid  nc8)
tsset firmnc8 year
gen Zint = exportnc8*ln(exportnc8)
gen Zintlag = L.exportnc8*ln(L.exportnc8)
*
gen exportlag = L.exportnc8
drop if Zint==. | Zintlag ==.
*
egen Nprodconst= count(Zint), by(firmid year)
egen Nprodconstlag= count(Zintlag), by(firmid year)
*
egen Z= sum(Zint), by(firmid year)
egen Zlag= sum(Zintlag), by(firmid year)
egen X= sum(exportnc8), by(firmid year)
egen Xlag= sum(exportlag), by(firmid year)
*
*
label var theilw "Theil index on worldwide sales"
label var entropw "Entropy on worldwide sales"
label var herfw "Herfindhal index on worldwide sales"
*
bysort firmid year : keep if _n == 1
*
gen theilw_dconst =  Z/X - Zlag/Xlag - ln(X) + ln(Xlag)
replace theilw_dconst = . if (Nprodconst!=Nprodconstlag) |  Nprodconst<3
label var theilw_dconst "local theil change (holding number of products constant"
*
tsset firmid year
gen theilw_d1 = D.theilw
reg theilw_dconst theilw_d1
*
rename Nprodconst Nprodconstw
keep firmid year theilw theilw0 theilw_dconst entropw herfw Nprodconstw atk*w
sort firmid year
save temp4,replace
*

********************************************
*5) merging back all skewness measures
use temp,clear
sort firmid year iso2_d isic
merge  firmid year iso2_d isic using temp2
tab _merge
drop _merge
sort firmid year
merge  firmid year using temp3
tab _merge
drop _merge
sort firmid year
merge  firmid year using temp4
tab _merge
drop _merge
*
forvalues x=1(1)5{
gen nbnc8`x' =  nbnc8^`x'
gen nbnc8iso2`x' =  nbnc8iso2^`x'
gen nbnc8iso2isic`x' =  nbnc8iso2isic^`x'
}
global nbnc8 " nbnc8  nbnc82 nbnc83"
global nbnc8iso2 " nbnc8iso2  nbnc8iso22 nbnc8iso23"
global nbnc8iso2isic " nbnc8iso2isic  nbnc8iso2isic2 nbnc8iso2isic3"
*
save MMO_C_regfile,replace
*
*
*******************************
*now merge with gravity data
sort iso2_d year
merge iso2_d year using fragrav
tab _merge
tab iso2_d if _merge == 1
*some crazy countries like antartica and al.
drop if _merge !=3
drop _merge
rename iso3_d iso
***********************************
*then merge with rmp and sp and rho
sort iso year
merge iso year using rmp
tab _merge
tab iso if _merge == 1 & year<2004
drop if _merge == 2
drop _merge
sort iso year
merge iso year using sp_rv
tab _merge
tab iso if _merge == 1 & year<2004
drop if _merge == 2
drop _merge lsp_rv_alt lrmp_rv_iv2
sort iso year
merge iso year using MMO_rho
tab _merge
tab iso if _merge == 1 & year<2004 & year > 1997
drop if _merge == 2
drop _merge
*
*********************************
*generate garavity-type variables
gen lgrav = ln(gdp_d/distw)
gen ld = ln(distw)
gen byte dbin1 = distw<=500
gen byte dbin2 = distw>500 & distw<=1000
gen byte dbin3 = distw>1000 & distw<=5000
gen byte dbin4 = distw>5000 & distw<=10000
gen byte dbin5 = distw>10000 & distw!=.
label var dbin1 "Distance <500km"
label var dbin2 "500km< Distance <1000km"
label var dbin3 "1000km< Distance <5000km"
label var dbin4 "5000km< Distance <10000km"
label var dbin5 "1000km< Distance"
gen lgdp = ln(gdp_d)
gen lgdpcap = ln(gdp_d/pop_d)
gen lpop = ln(pop_d)
label var lpop "ln population"
gen lnbx = ln(nbexporters)
label var lgrav "easyness of market"
gen distrel = ld-remote_d
label var distrel "relative distance (ln(distance/remote))"
*
rename iso iso3_d
*
gen  byte euro = (iso3_d=="AUT" |iso3_d=="BEL" |iso3_d=="DEU" |iso3_d=="FIN" |iso3_d=="FRA" |iso3_d=="IRL" |iso3_d=="ITA" |iso3_d=="LUX"|iso3_d=="NLD" |iso3_d=="PRT" |iso3_d=="ESP" |iso3_d=="AND" |iso3_d=="SMR" |iso3_d=="MCO")
gen  byte euro_in = euro & (year>=1999)
label var euro "euro dest."
label var euro_in "euro dest. post 1999"
*
drop ldistint impFRA impWLD frashare distrel lnbx lrmp_rv lfmp_rv lrmp_rv_iv1 remote_d distw
*************************************
*We need to trim super extreme values
gen ratio = exp(lratio)
gen ratiow = exp(lratiow)
gen ratiowc = exp(lratiowc)
count if ratiow > 1000000 & ratiow!=.
count if ratiowc > 1000000 & ratiowc!=.
count if ratio > 1000000 & ratio!=.
drop if ratiow > 1000000 & ratiow!=.
drop if ratiowc > 1000000 & ratiowc!=.
drop if ratio > 1000000 & ratio!=.
*
compress
save MMO_C_regfile,replace
*
**********************************************************************
*ADD MNES info (this is done by mnes.do in MMO directory, and kept only
*for 2002 (could be time varying, but not after 2002)
use MMO_C_regfile, clear
sort firmid iso2_d
merge firmid iso2_d using mnes_sirtg
tab _merge
drop if _merge == 2
rename nbaff nbaff1
replace nbaff1 = 0 if _merge == 1
drop _merge
sort firmid iso2_d
merge firmid iso2_d using mnes_siren
tab _merge
drop if _merge == 2
drop _merge
replace nbaff  = nbaff1 if nbaff == . & nbaff1 !=.
count if nbaff == .
drop nbaff1
egen mne = sum(nbaff), by(firmid)
replace mne = 1 if mne > 0 & mne!=.
label var mne "firm is a multinational in 2002"
save MMO_C_regfile,replace
*
erase temp.dta
*
*
**************
*Trade Shocks*
**************
*
clear all
set mem 4g
*keep sales only
use MMO_C_regfilefull, clear
qui bysort firmid year : keep if _n == 1
keep firmid year sales exp_sales
sort firmid year
save firmsales,replace
*
use MMO_C_regfilefull, clear
*getting iso3 code
sort iso2_d
merge iso2_d using iso23
tab _merge
keep if _merge ==3
drop _merge
*
*getting gravity data
sort iso2_d year
merge iso2_d year using fragrav
tab _merge
drop if _merge !=3
drop _merge
*
keep firmid nc8 iso2_d year isic export
sum
global yearrangedim "firmid"
egen year_first = min(year), by($yearrangedim)
egen year_last = max(year), by($yearrangedim)
egen firmiso = group(firmid nc8 iso2_d)
tsset firmiso year
tsfill, full
count
count if export == .
save temp00, replace
use temp00, clear
keep if export !=.
qui by firmiso: keep if _n == 1
count if firmiso == .
keep  firmid nc8 iso2_d firmiso year_first year_last isic
sort firmiso
save temp01, replace
use temp00, clear
merge m:1 firmiso using temp01, update
replace export = 0 if export == .
sum
drop _merge
keep if year>=year_first & year<=year_last
*
*getting iso3 code
sort iso2_d
merge iso2_d using iso23
tab _merge
keep if _merge ==3
drop _merge
*
*getting gravity data
sort iso2_d year
merge iso2_d year using fragrav
tab _merge
drop if _merge !=3
drop _merge
*
gen hs6 = int(nc8/100)
collapse (sum) export (mean) gdp_d pop_d year_first year_last isic, by(firmid year hs6 iso3_d)
save temp00, replace
*
rename iso3_d iso_d
*BACI:
sort iso_d hs6 year
merge iso_d hs6 year using baci_china
tab _merge
drop if _merge == 2
drop _merge
*UNIDO:
sort iso_d isic year
merge iso_d isic year using data/tradeprod_small
tab _merge
drop if _merge == 2
*
egen firmiso = group(firmid hs6 iso_d)
egen totexp = sum(export), by(firmid year)
gen gdpcap_d = gdp_d / pop_d
*
save temp00, replace
*
use temp00, clear
tsset firmiso year
sort firmiso year
* This macro sets year of export to use as "1st" year
global yearfirst "1"
qui by firmiso : gen shrexp1st = (export[$yearfirst] / totexp[$yearfirst])
gen shrexpcur = (export/totexp)
gen shrexplag = (L1.export/L1.totexp)
replace shrexplag = 0 if L1.totexp == 0
replace shrexpcur = 0 if totexp == 0
*
egen shrexpcuravg = mean(shrexpcur), by(firmid year iso_d isic)
egen shrexplagavg = mean(shrexplag), by(firmid year iso_d isic)
egen shrexp1stavg = mean(shrexp1st), by(firmid year iso_d isic)
*
sum shrexpcur, d
sum shrexp1st, d
sum shrexplag, d
sum shrexpcuravg, d
sum shrexplagavg, d
sum shrexp1stavg, d
*
label var shrexpcur "share of country/hs6 in total year export of firm - current"
label var shrexpcuravg "share of country/hs6 in total year export of firm (avg. per firm/dest/year) - current"
label var shrexplag "share of country/hs6 in total year export of firm - lagged"
label var shrexplagavg "share of country/hs6 in total year export of firm (avg. per firm/dest/year) - lagged"
label var shrexp1st "share of country/hs6 in total year export of firm - first year in sample"
label var shrexp1stavg "share of country/hs6 in total year export of firm (avg. per firm/dest/year) - first year in sample"
*
*never use first year(s)
replace shrexp1st = . if year <= year_first - 1 + $yearfirst
replace shrexp1stavg = . if year <= year_first - 1 + $yearfirst
*
*Macros to be used for both levels and FD of trade shocks:
global shrexpFD "shrexplagavg"
global shrexpFE "shrexp1stavg"
*
rename conso v_conso
rename intflow v_intflow
foreach x in FRA FRA_isic CHN DEU WLD WLD_isic conso intflow{
replace v_`x' = 0 if v_`x' == .
}
*
gen v_WLDt= v_WLD
replace v_WLD = v_WLD - v_FRA
gen v_WLDt_isic=v_WLD_isic
replace v_WLD_isic = v_WLD_isic - v_FRA_isic
*
replace  v_conso = conso - v_FRA_isic
replace v_WLD_isic=0 if v_WLD_isic<0
replace v_conso=0 if v_conso<0
replace v_intflow=0 if v_intflow<0
*
foreach x in FRA FRA_isic CHN DEU WLD WLDt WLD_isic WLDt_isic conso intflow{
replace v_`x' = v_`x' + 1
}
************************************************
*Construct variables to be collapsed for shocks*
*levels
gen shrexptimesGDP1 = $shrexpFE * ln(gdp_d)
gen shrexptimesGDPc1 = $shrexpFE * ln(gdpcap_d)
gen shrexptimesCHN1 = $shrexpFE * ln(v_CHN)
gen shrexptimesDEU1 = $shrexpFE * ln(v_DEU)
gen shrexptimesWLD1  = $shrexpFE * ln(v_WLD)
gen shrexptimesWLDi1  = $shrexpFE * ln(v_WLD_isic)
gen shrexptimesWLDt1  = $shrexpFE * ln(v_WLDt)
gen shrexptimesWLDit1  = $shrexpFE * ln(v_WLDt_isic)
gen shrexptimesNBCTRYi1  = $shrexpFE * ln(nbctry_WLD_isic)
gen shrexptimesCONSO1 = $shrexpFE * ln(v_conso)
gen shrexptimesINTFLOW1 = $shrexpFE * ln(v_intflow)
gen shrexptimesFIRMS1 = $shrexpFE * ln(firms)
*FD, DH style
gen shrexptimesGDP2 = $shrexpFD * 2*(D1.gdp_d/(gdp_d+L1.gdp_d))
gen shrexptimesGDPc2 = $shrexpFD * 2*(D1.gdpcap_d/(gdpcap_d+L1.gdpcap_d))
gen shrexptimesCHN2 = $shrexpFD * 2*(D1.v_CHN/(v_CHN+L1.v_CHN))
gen shrexptimesDEU2 = $shrexpFD * 2*(D1.v_DEU/(v_DEU+L1.v_DEU))
gen shrexptimesWLD2 = $shrexpFD * 2*(D1.v_WLD/(v_WLD+L1.v_WLD))
gen shrexptimesWLDi2 = $shrexpFD * 2*(D1.v_WLD_isic/(v_WLD_isic+L1.v_WLD_isic))
gen shrexptimesNBCTRYi2 = $shrexpFD * 2*(D1.nbctry_WLD_isic/(nbctry_WLD_isic+L1.nbctry_WLD_isic))
gen shrexptimesCONSO2 = $shrexpFD * 2*(D1.v_conso/(v_conso+L1.v_conso))
gen shrexptimesINTFLOW2 = $shrexpFD * 2*(D1.v_intflow/(v_intflow+L1.v_intflow))
gen shrexptimesFIRMS2 = $shrexpFD * 2*(D1.firms/(firms+L1.firms))
*FD
gen lgdp_d = ln(gdp_d)
gen lgdpcap_d = ln(gdpcap_d)
gen lv_WLD=ln(v_WLD)
gen lv_WLDi=ln(v_WLD_isic)
gen shrexptimesGDP3 = $shrexpFD * D1.lgdp_d
gen shrexptimesGDPc3 = $shrexpFD * D1.lgdpcap_d
gen shrexptimesWLD3  = $shrexpFD * D1.lv_WLD
gen shrexptimesWLDi3  = $shrexpFD * D1.lv_WLDi
*******************************************************
*Create the trade shocks purged from dest-year effects:
*First choose over which dimension to demean: 
egen destyear = group(iso_d isic year)
xtreg shrexptimesWLD1, fe i(destyear)
predict shrexptimesWLDdm1, e
corr shrexptimesWLD1  shrexptimesWLDdm1
xtreg shrexptimesWLD2, fe i(destyear)
predict shrexptimesWLDdm2, e
corr shrexptimesWLD2  shrexptimesWLDdm2
*
sum shrexptimes*
*v2
gen lvwLD=  ln(v_WLD)
egen lvwLD_mean=mean(lvwLD), by(destyear) 
gen shrexptimesWLD1_dm=shrexptimesWLD1-$shrexpFE * lvwLD_mean 
pwcorr shrexptimesWLD1 shrexptimesWLD1_dm shrexptimesWLDdm1
tsset firmiso year
gen DWvwld =  2*(D1.v_WLD/(v_WLD+L1.v_WLD))
egen DWvwld_mean=mean(DWvwld), by(destyear) 
gen shrexptimesWLD2_dm=shrexptimesWLD2-$shrexpFD * DWvwld_mean 
pwcorr shrexptimesWLD2 shrexptimesWLD2_dm shrexptimesWLDdm2
drop  lvwLD* DWvwld*
*
drop shrexptimesWLDdm1 shrexptimesWLDdm2 
rename shrexptimesWLD1_dm shrexptimesWLDdm1
rename shrexptimesWLD2_dm shrexptimesWLDdm2
*
save temp_tradeshocks,replace
*

***************************************
*Add Trade shocks: shocks by firm-year*
***************************************
clear
set more off
use temp_tradeshocks, clear
*
collapse (sum)  shrexp* export  ///
(count) missGDP1=shrexptimesGDP1 missGDP2=shrexptimesGDP2 missGDP3=shrexptimesGDP3 ///
		missGDPc1=shrexptimesGDPc1 missGDPc2=shrexptimesGDPc2 missGDPc3=shrexptimesGDPc3 ///
		missCHN1=shrexptimesCHN1 missDEU1=shrexptimesDEU1 missWLD1=shrexptimesWLD1 missWLDt1=shrexptimesWLDt1 missCONSO1=shrexptimesCONSO1  ///
        missCHN2=shrexptimesCHN2 missDEU2=shrexptimesDEU2 missWLD2=shrexptimesWLD2 missCONSO2=shrexptimesCONSO2 ///
		missWLD3=shrexptimesWLD3 missWLDdm1=shrexptimesWLDdm1 missWLDdm2=shrexptimesWLDdm2 ///
		missWLDi1=shrexptimesWLDi1 missWLDit1=shrexptimesWLDit1 missWLDi2=shrexptimesWLDi2 missWLDi3=shrexptimesWLDi3 ///
		missNBCTRYi1=shrexptimesNBCTRYi1 missNBCTRYi2=shrexptimesNBCTRYi2 ///
		missINTFLOW1=shrexptimesINTFLOW1 missINTFLOW2=shrexptimesINTFLOW2 ///
		missFIRMS1=shrexptimesFIRMS1 missFIRMS2=shrexptimesFIRMS2 ///
		misscur=shrexpcur misslag=shrexplag miss1st=shrexp1st ///
		misscuravg=shrexpcuravg misslagavg=shrexplagavg miss1stavg=shrexp1stavg ///
		missexport=export, by(firmid year) fast

 foreach x in GDP1 GDP2 GDP3 GDPc1 GDPc2 GDPc3 CHN1 DEU1 WLD1 WLDi1 WLDt1 WLDit1 WLDdm1 WLDdm2 CHN2 DEU2 WLD2 WLDi2 NBCTRYi1 NBCTRYi2 ///
 CONSO1 CONSO2 INTFLOW1 INTFLOW2 FIRMS1 FIRMS2 WLD3 WLDi3{
replace shrexptimes`x' = . if miss`x'==0
}
*
corr shrexptimesWLD1  shrexptimesWLDdm1
corr shrexptimesWLD2  shrexptimesWLDdm2
*
 foreach x in cur lag 1st {
replace shrexp`x' = . if miss`x'==0
replace shrexp`x'avg = . if miss`x'avg==0
}
replace export = . if missexport==0
drop miss*
rename export totexport
label var totexport "total export by firm/year"
sort firmid year
merge firmid year using firmsales
tab _merge
drop _merge
rename exp_sales totexport_brn
label var totexport_brn "total export by firm/year (brn source)"
*
global totexport "totexport_brn"
sort firmid year
qui by firmid : gen exportintens1st = ($totexport[$yearfirst]/sales[$yearfirst])
tsset firmid year
gen exportintenslag = (L1.$totexport/L1.sales)
gen exportintenscur = ($totexport/sales)
sum exportintenscur, d
sum exportintenslag, d
sum exportintens1st, d
*those come from different datasets, and sometimes totexport>sales
foreach x in cur lag 1st {
replace exportintens`x'= exportintens`x'/1000 if $totexport==totexport
replace exportintens`x' = 1 if exportintens`x'>1 & exportintens`x'!=.
replace exportintens`x' = 0 if exportintens`x'<0 & exportintens`x'!=.
}
pwcorr exportintens*
*
rename sales sales_tradeshock
drop shrexp*avg
sort firmid year
save tradeshocks,replace
*
pwcorr shrexptimesWLD* shrexptimesCONSO*
*
************************************************
*Add Trade shocks: shocks by firm-iso-isic-year*
************************************************
use temp_tradeshocks, clear
*
collapse (sum)  shrexp* export ///
(count) missGDP1=shrexptimesGDP1 missGDP2=shrexptimesGDP2 missGDP3=shrexptimesGDP3 ///
		missGDPc1=shrexptimesGDPc1 missGDPc2=shrexptimesGDPc2 missGDPc3=shrexptimesGDPc3 ///
		missCHN1=shrexptimesCHN1 missDEU1=shrexptimesDEU1 missWLD1=shrexptimesWLD1 missWLDt1=shrexptimesWLDt1 missCONSO1=shrexptimesCONSO1  ///
        missCHN2=shrexptimesCHN2 missDEU2=shrexptimesDEU2 missWLD2=shrexptimesWLD2 missCONSO2=shrexptimesCONSO2 ///
		missWLD3=shrexptimesWLD3 ///
		missWLDi1=shrexptimesWLDi1 missWLDit1=shrexptimesWLDit1 missWLDi2=shrexptimesWLDi2 missWLDi3=shrexptimesWLDi3 ///
		missNBCTRYi1=shrexptimesNBCTRYi1 missNBCTRYi2=shrexptimesNBCTRYi2 ///
		missINTFLOW1=shrexptimesINTFLOW1 missINTFLOW2=shrexptimesINTFLOW2 ///
		missFIRMS1=shrexptimesFIRMS1 missFIRMS2=shrexptimesFIRMS2 ///
		misscur=shrexpcur misslag=shrexplag miss1st=shrexp1st ///
		misscuravg=shrexpcuravg misslagavg=shrexplagavg miss1stavg=shrexp1stavg ///
		missexport=export, by(firmid iso_d year isic) fast

 foreach x in GDP1 GDP2 GDP3 GDPc1 GDPc2 GDPc3 CHN1 DEU1 WLD1 WLDi1 WLDt1 WLDit1 CHN2 DEU2 WLD2 WLDi2 NBCTRYi1 NBCTRYi2 ///
 CONSO1 CONSO2 INTFLOW1 INTFLOW2 FIRMS1 FIRMS2 WLD3 WLDi3{
replace shrexptimes`x' = . if miss`x'==0
}
 foreach x in cur lag 1st {
replace shrexp`x' = . if miss`x'==0
replace shrexp`x'avg = . if miss`x'avg==0
}
replace export = . if missexport==0
drop miss*
*
*rescaling such that shares sum to 1 for the collapse level: firmid iso_d year isic
 foreach x in GDP1 GDPc1 CHN1 DEU1 WLD1 WLDi1 WLDt1 WLDit1  NBCTRYi1  CONSO1  INTFLOW1  FIRMS1  {
replace shrexptimes`x' = shrexptimes`x'/$shrexpFE
}
 foreach x in  GDP2 GDP3 GDPc2 GDPc3 WLD3 WLDi3 CHN2 DEU2 WLD2 WLDi2  NBCTRYi2 CONSO2 INTFLOW2 FIRMS2 {
replace shrexptimes`x' = shrexptimes`x'/$shrexpFD
}
*
drop shrexp*avg
*now we need total exports by firm/year
egen  totexport = sum(export), by(firmid year)
label var totexport "total export by firm/year"
sort firmid iso_d year isic
save tradeshocksiso,replace



